<?php

require_once 'Pipes/Exception.php';
require_once 'PipesImpl/Visitor.php';

class Pipes_Helpers_Grid_AttributeParser implements PipesImpl_Visitor
{
    private $_variables = array();
    public function __construct(array $variables)
    {
        $this->_variables = $variables;
    }
    
    public function visit(PipesImpl_Visitable $visitable)
    {
        if (!$visitable instanceof Pipes_Xml_Element) {
            throw new Pipes_Exception('ERR_INVALID_VISITOR');
        }
        
        extract($this->_variables);
        
        foreach ($visitable->attributes as $attribute) {
            preg_match_all('/\{\$([^-]+)->([^\}]+)\}/', $attribute->nodeValue, $matches);
            foreach ($matches[0] as $matchKey => $match) {
                $search = $match;
                
                $replaceObject = $matches[1][$matchKey];
                $replaceProp = $matches[2][$matchKey];
                $replace = $$replaceObject->$replaceProp;
                
                $attribute->nodeValue = str_replace($search, $replace, $attribute->nodeValue);
            }
        }
    }
}
